package MyFramePro.Handler;

import MyFramePro.Domain.ResponseResult;
import MyFramePro.Enums.AppHttpCodeEnum;
import MyFramePro.Exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 这里是统一异常的配置类
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 获取自定义异常
     */
    @ExceptionHandler(value = SystemException.class)    //只要是抛出这类异常，都会进入该方法进行异常信息返回
    public ResponseResult systemExceptionHandler(SystemException exception){
        //1.打印异常信息
        log.error("出现异常："+exception);

        //2.从异常对象中获取提示信息，封装返回
        return ResponseResult.errorResult(exception.getCode(),exception.getMsg());
    }

    /**
     * 获取全局异常
     */
    @ExceptionHandler(value = Exception.class)
    public ResponseResult exceptionHandler(Exception exception){
        //1.打印异常信息
        log.error("出现异常："+exception);

        //2.从异常对象中获取提示信息，封装返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),exception.getMessage());
    }

}
